/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.business.manager;

import cz.cvut.fel.taskpro.business.imanager.ITeamBusiness;
import cz.cvut.fel.taskpro.business.isupport.IBusinessRuleValidator;
import cz.cvut.fel.taskpro.business.support.BusinessRuleException;
import cz.cvut.fel.taskpro.data.entity.Assignment;
import cz.cvut.fel.taskpro.data.entity.AssignmentSubmit;
import cz.cvut.fel.taskpro.data.entity.GroupTaskPro;
import cz.cvut.fel.taskpro.data.entity.Kantor;
import cz.cvut.fel.taskpro.data.entity.Student;
import cz.cvut.fel.taskpro.data.entity.Team;
import cz.cvut.fel.taskpro.data.imanager.IAssignmentData;
import cz.cvut.fel.taskpro.data.imanager.ITeamData;
import cz.cvut.fel.taskpro.data.imanager.IUserData;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;

/**
 * This class implement #{@link ITeamBusiness} interface
 *
 * @author Tom
 * @version 1.0. beta
 */
@DeclareRoles({"admin", "kantor", "student"})
@Stateless
public class TeamBusiness implements ITeamBusiness, Serializable {

    @Inject
    private ITeamData teamData;
    @Inject
    private IAssignmentData assignmentData;
    @Inject
    private IUserData userData;
    @EJB
    private IBusinessRuleValidator validator;

    public TeamBusiness() {
    }

    @RolesAllowed("kantor")
    @Override
    public void addTeam(Team newTeam) throws BusinessRuleException {
        validator.validateAddTeam(newTeam);
        teamData.addTeam(newTeam);
    }

    @RolesAllowed({"kantor", "student"})
    @Override
    public List<Team> getTeamsByKantor(Kantor kantor) {
        return teamData.getTeamByKantor(kantor);
    }

    @RolesAllowed({"kantor", "student"})
    @Override
    public List<Team> getTeamsByGroup(List<GroupTaskPro> groups) {
        return teamData.getTeamByGroup(groups);
    }

    @RolesAllowed({"kantor", "student"})
    @Override
    public List<Team> getTeamsByStudent(Student student) {
        return teamData.getTeamsByStudent(student);
    }

    @RolesAllowed("kantor")
    @Override
    public void addStudentToTeam(Team oldTeam, Team teamToMerge) throws BusinessRuleException {
        validator.validateTeam(teamToMerge);
        Iterator stIT = teamToMerge.getmStudents().iterator();
        while (stIT.hasNext()) {
            Student cuStudent = (Student) stIT.next();
            cuStudent.addTeam(oldTeam);
            oldTeam.addStudent(cuStudent);
        }
        teamData.updateTeam(oldTeam);

    }

    @RolesAllowed("kantor")
    @Override
    public void removeTeam(Team teamToRemove) throws BusinessRuleException {
        validator.validateRemove(teamToRemove);
        List<Assignment> assignmentsToDelete = new ArrayList<Assignment>();
        List<Assignment> assignmentsToTeam = teamToRemove.getmAssignments();
        Iterator asIt = assignmentsToTeam.iterator();
        teamToRemove.setmAssignments(null);
        while (asIt.hasNext()) {
            Assignment currAssignment = (Assignment) asIt.next();
            List<Team> teamsOnAssignment = currAssignment.getmTeams();
            teamsOnAssignment.remove(teamToRemove);
            currAssignment.setmTeams(teamsOnAssignment);
            if (currAssignment.getmTeams().isEmpty()) {
                assignmentsToDelete.add(currAssignment);
            }
            assignmentData.updateAssignment(currAssignment);
        }
        List<Student> studentsInTeam = teamToRemove.getmStudents();
        Iterator stIt = studentsInTeam.iterator();
        while (stIt.hasNext()) {
            Student currStudent = (Student) stIt.next();
            List<Team> studentsTeam = currStudent.getTeam();
            studentsTeam.remove(teamToRemove);
            userData.updateUser(currStudent);
        }
        teamToRemove.setmStudents(null);
        teamData.updateTeam(teamToRemove);
        teamData.removeTeam(teamToRemove);
    }

    @RolesAllowed("kantor")
    @Override
    public void removeStudentsFromTeam(Team teamInStudents, List<Student> studentsToRemove) throws BusinessRuleException {
        validator.validateRemove(teamInStudents, studentsToRemove);
        List<Student> remainStudent = teamInStudents.getmStudents();
        if (remainStudent.size() == studentsToRemove.size()) {
            removeTeam(teamInStudents);
        } else {
            Iterator studentsToRemoveIt = studentsToRemove.iterator();
            while (studentsToRemoveIt.hasNext()) {
                Student currStudent = (Student) studentsToRemoveIt.next();
                List<Team> studentsTeam = currStudent.getTeam();
                studentsTeam.remove(teamInStudents);
                currStudent.setTeam(studentsTeam);
                userData.updateUser(currStudent);
                remainStudent.remove(currStudent);
            }
            teamInStudents.setmStudents(remainStudent);
            teamData.updateTeam(teamInStudents);
        }

    }
}
